home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -seriously_amiga- / misc / pdflib / pdfgraph.c < prev    next >
C/C++ Source or Header  |  1999-03-24  |  3KB  |  150 lines

  1. /* pdfgraph.c
  2.  * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
  3.  *
  4.  * A micro language for drawing PDF graphics
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10.  
  11. #ifdef POSIX
  12. #include <unistd.h>
  13. #endif
  14.  
  15. #ifdef DOS
  16. #include <process.h>
  17. #endif
  18.  
  19. #ifdef NeXT
  20. #include <libc.h>    /* for getopt(), optind, optarg */
  21. #endif
  22.  
  23. #include "pdf.h"
  24.  
  25. static void
  26. usage(void)
  27. {
  28.     fprintf(stderr, "pdfgraph - draw PDF graph. (C) Thomas Merz 1997-98\n");
  29.     fprintf(stderr, "usage: pdfgraph [options] [datafile]\n");
  30.     fprintf(stderr, "Available options:\n");
  31.     fprintf(stderr, "-b         binary mode (default: ASCII)\n");
  32.     fprintf(stderr, "-o filename    PDF output file name\n");
  33.  
  34.     exit(1);
  35. }
  36.  
  37. #define BUFLEN 512
  38.  
  39. void
  40. main(int argc, char *argv[])
  41. {
  42.     PDF_info    *info;
  43.     char    buf[BUFLEN], *cmd;
  44.     FILE    *pdffile = NULL, *datafile = stdin;
  45.     PDF        *p;
  46.     int        opt;
  47.     float    page_width = 595, page_height = 842;
  48.     float    x, y, gray;
  49.     float    red, green, blue;
  50.  
  51.     info        = PDF_get_info();
  52.     info->Title        = "stdin";
  53.     info->Creator       = "pdfgraph";
  54.     info->binary_mode    = false;
  55.  
  56.     while ((opt = getopt(argc, argv, "bo:")) != -1)
  57.     switch (opt) {
  58.         case 'b':
  59.         info->binary_mode = true;
  60.         break;
  61.         
  62.         case 'o':
  63.         pdffile = fopen(optarg, WRITEMODE);
  64.         if (pdffile == NULL) {
  65.             fprintf(stderr, 
  66.                 "Error: cannot open output file %s.\n", optarg);
  67.             usage();
  68.         }
  69.         break;
  70.  
  71.         case '?':
  72.         default:
  73.         usage();
  74.     }
  75.  
  76.     if (pdffile == (FILE *) NULL)
  77.     usage();
  78.  
  79.     if (optind < argc) {
  80.     info->Title    = argv[optind];
  81.     if ((datafile = fopen(argv[optind], READMODE)) == NULL) {
  82.         fprintf(stderr, "Error: cannot open data file %s.\n",argv[optind]);
  83.         exit(1);
  84.     }
  85.     } else
  86.     usage();
  87.  
  88.     p = PDF_open(pdffile, info);
  89.  
  90.     PDF_begin_page(p, page_width, page_height);
  91.  
  92.     while ((cmd = fgets(buf, BUFLEN, datafile)) != NULL) {
  93.     switch (cmd[0]) {
  94.     case 'M':
  95.         if (sscanf(buf+1, "%f %f", &x, &y) != 2) {
  96.         fprintf(stderr, "Error in line: %s", buf);
  97.         continue;
  98.         }
  99.         PDF_moveto(p, x, y);
  100.         break;
  101.  
  102.     case 'L':
  103.         if (sscanf(buf+1, "%f %f", &x, &y) != 2) {
  104.         fprintf(stderr, "Error in line: %s", buf);
  105.         continue;
  106.         }
  107.         PDF_lineto(p, x, y);
  108.         break;
  109.  
  110.     case 'S':
  111.         PDF_stroke(p);
  112.         break;
  113.  
  114.     case 'f':
  115.         PDF_fill(p);
  116.         break;
  117.  
  118.     case 'F':
  119.         PDF_fill_stroke(p);
  120.         break;
  121.  
  122.     case 'g':
  123.         if (sscanf(buf+1, "%f", &gray) != 1) {
  124.         fprintf(stderr, "Error in line: %s", buf);
  125.         continue;
  126.         }
  127.         PDF_setgray(p, gray);
  128.         break;
  129.  
  130.     case 'C':
  131.         if (sscanf(buf+1, "%f %f %f", &red, &green, &blue) != 3) {
  132.         fprintf(stderr, "Error in line: %s", buf);
  133.         continue;
  134.         }
  135.         PDF_setrgbcolor(p, red, green, blue);
  136.         break;
  137.  
  138.     case '%':
  139.     default:
  140.         break;;
  141.     }
  142.     }
  143.  
  144.     PDF_end_page(p);
  145.     PDF_close(p);
  146.  
  147.     fclose(datafile);
  148.     exit(0);
  149. }
  150.